﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace DotNetUniLodz.Models
{
    public class UserModel : User
    {
        static DotNetULEntities db = new DotNetULEntities();
        public UserModel() : base() { }

        [Required]
        [MinLength(3, ErrorMessage = "Logi musi posiadać minimum 3 znaki!")]
        [DisplayName("Login")]
        public new string User_login { get; set; }

        [Required]
        [DisplayName("Imię")]
        public new string User_firstName { get; set; }

        [DisplayName("Nawisko")]
        public new string User_lastName { get; set; }

        [Required]
        [EmailAddress(ErrorMessage = "Podaj poprawny adres e-mail")]
        [DisplayName("Adres e-mail")]
        public new string User_mail { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [MinLength(6, ErrorMessage = "Hasło musi posiadać minimum 6 znaków!")]
        [DisplayName("Hasło")]
        public new string User_pass { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [System.Web.Mvc.Compare("User_pass", ErrorMessage = "Hasło i powtórzone hasło muszą być identyczne")]
        [DisplayName("Powtórzone hasło")]
        public string User_confirmPassword { get; set; }

        /// <summary>
        /// Pobiera Usera z EF, gdyż nasz UserModel przesłania klasę User
        /// </summary>
        public User User
        {
            get
            {
                User user = new User()
                {
                    User_login = this.User_login,
                    User_firstName = this.User_firstName,
                    User_lastName = this.User_lastName,
                    User_mail = this.User_mail,
                    User_pass = this.User_pass
                };
                return user;
            }
        }

        /// <summary>
        /// Pobiera aktualnie zalogowanego użytkownika
        /// </summary>
        /// <param name="userName">Nazwa użytkownika, najlepiej z User.Identity.Name</param>
        /// <returns>Aktualnie zalogowany użytkownik, albo null</returns>
        public static User GetCurrentUser(string userName)
        {
            return db.Users.FirstOrDefault(u => u.User_login == userName);
        }
    }
}